<?php

class ComplaintController extends \Base\AbstractController {

    /**
     * @desc 订单投诉列表
     *
     */
    public function listAction() {
        $request = $this->getRequest();
        $class   = (int)$request->get('class');
        $keyword = trim((string)$request->get('keyword'));
        $status  = (int)$request->get('status');
        $where   = [];
        $return  = [];

        if(!empty($class) && !empty($keyword)) {

            if($class == 1) {
                $first = ['order_id' => $keyword];
            }
        }

        if(!empty($status)) {
            $second = ['status' => $status];
        }

        //组装查询语句
        if(!empty($first) && !empty($second)) {
            $where = array_merge($first, $second);
        } elseif(!empty($first) || !empty($second)) {
            empty($first) ? $where = $second : $where = $first;
        }

        //搜索
        $page = new \Lib\Paginator('complaint');
        $page->where($where);
        $this->assign('page', $page->getPage());
        $this->assign('perPage', $page->getPerpage());
        $this->assign('count', $page->count());
        $models       = $page->lists();
        $orderMapper  = Mapper\OrderModel::getInstance();
        $memberMapper = Mapper\MemberModel::getInstance();

        foreach($models as $model) {
            $data  = $model->toArray();
            $order = $orderMapper->findById($data['order_id']);

            if($order instanceof \OrderModel) {
                $data['from_uid'] = $order->getFrom_uid();
                $data['to_uid']   = $order->getTo_uid();

                $data['from_name'] = urldecode($memberMapper->findById($data['from_uid'])->getNick_name());
                $data['to_name']   = urldecode($memberMapper->findById($data['to_uid'])->getNick_name());
            }

            $return[] = $data;
        }

        $this->assign('lists', $return);

    }

}
